package com.yidu.hbatsft.action;


import com.yidu.utils.ExportWordTools.XWPFHelper;
import com.yidu.utils.ExportWordTools.XWPFHelperTable;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;


public class RivalExportWord {



    public int type = 1;
    private XWPFHelperTable xwpfHelperTable = null;
    private XWPFHelper xwpfHelper = null;

    public RivalExportWord(int type) {
        this.type = type;
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();
    }

    public RivalExportWord() {
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();
    }
    /**
     * 创建好文档的基本 标题，表格  段落等部分
     * @return
     * @Author Huangxiaocong 2018年12月16日
     */
    public XWPFDocument createXWPFDocument() {
        XWPFDocument doc = new XWPFDocument();
        createTitleParagraph(doc);
        int count = type*4;
        createTableParagraph(doc,6+count, 6);



        return doc;
    }
    /**
     * 创建表格的标题样式
     * @param document
     * @Author Huangxiaocong 2018年12月16日 下午5:28:38
     */
    public void createTitleParagraph(XWPFDocument document) {
        XWPFParagraph titleParagraph = document.createParagraph();    //新建一个标题段落对象（就是一段文字）
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
        XWPFRun titleFun = titleParagraph.createRun();    //创建文本对象
        //titleFun.setText(titleName); //设置标题的名字
        titleFun.setBold(true); //加粗
        titleFun.setColor("000000");//设置颜色
        titleFun.setFontSize(22);    //字体大小
        titleFun.setFontFamily("方正小标宋简体");//设置字体
        //titleFun.addBreak();    //换行  当前行切到下一行
    }
    /**
     * 设置表格格式
     * @param document
     * @param rows
     * @param cols
     * @Author Huangxiaocong 2018年12月16日
     */
    public void createTableParagraph(XWPFDocument document, int rows, int cols) {
        //xwpfHelperTable.createTable(xdoc, rowSize, cellSize, isSetColWidth, colWidths)

        //新建一个表格 名字为infoTable
        XWPFTable infoTable = document.createTable(rows, cols);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable, "76000", STJc.CENTER);


        //设置指定宽度
        CTTbl ttbl = infoTable.getCTTbl();
        CTTblGrid tblGrid = ttbl.addNewTblGrid();
        int[] colWidths = new int[] {1000,1000,1000,1000,1000,1000};
        for (int i : colWidths) {
            CTTblGridCol gridCol = tblGrid.addNewGridCol();
            gridCol.setW(new BigInteger(i+""));
        }
        int count = type*4;
        for(int col = 0; col < 6+count; col++){
            if(col>0&&col<5){
                xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 1, 5);
            }else if (col%4==2&&col!=2){
                xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 0, 5);
            }else if ((col-1)%4==2||(col-2)%4==2){
                xwpfHelperTable.mergeCellsHorizontal(infoTable,col,1,2);
                xwpfHelperTable.mergeCellsHorizontal(infoTable,col,4,5);
            }else if(col==5){
                xwpfHelperTable.mergeCellsHorizontal(infoTable,col,1,2);
                xwpfHelperTable.mergeCellsHorizontal(infoTable,col,4,5);
            }
        }

        //设置表格样式
        List<XWPFTableRow> rowList = infoTable.getRows();
        for(int i = 0; i < rowList.size(); i++) {
            XWPFTableRow infoTableRow = rowList.get(i);
            List<XWPFTableCell> cellList = infoTableRow.getTableCells();
            for(int j = 0; j < cellList.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun cellParagraphRun = cellParagraph.createRun();
                cellParagraphRun.setFontSize(12);
                //设置背景颜色
                CTTcPr tcpr = cellList.get(j).getCTTc().addNewTcPr();
                if((i<5&&j%2==0)){
                    setBackColor(tcpr,"D7D7D7");
                }else if(i==5&&(j==0||j==3)){
                    setBackColor(tcpr,"D7D7D7");
                }else if(i%4==2&&i!=2){
                    setBackColor(tcpr,"D7D7D7");
                }else if(((i-1)%4==2||(i-2)%4==2)&&i!=3&&i!=4&&(j==0||j==3)){
                    setBackColor(tcpr,"D7D7D7");
                }else if (i>8&&j%2==0){
                    setBackColor(tcpr,"D7D7D7");
                }
            }
        }

        //设置表格高度
        xwpfHelperTable.setTableHeight(infoTable, 200, STVerticalJc.CENTER);
    }

    /**
     * 设置单元格背景颜色
     * @param tcpr
     */
    public void setBackColor(CTTcPr tcpr,String color){
        CTShd ctshd = tcpr.addNewShd();
        ctshd.setColor("auto");
        ctshd.setVal(STShd.CLEAR);
        ctshd.setFill(color);
    }

    /**
     * 往表格中填充数据
     * @param dataList
     * @param document
     * @throws IOException
     * @Author Huangxiaocong 2018年12月16日
     */
    public void exportCheckWord(Map<String, Object> dataList, XWPFDocument document) throws IOException {
        XWPFParagraph paragraph = document.getParagraphArray(0);
        XWPFRun titleFun = paragraph.getRuns().get(0);
        titleFun.setText(String.valueOf(dataList.get("TITLE")));
        List<List<Object>> tableData = (List<List<Object>>) dataList.get("TABLEDATA");
        XWPFTable table = document.getTableArray(0);
        fillTableData(table, tableData);
    }
    /**
     * 往表格中填充数据
     * @param table
     * @param tableData
     * @Author Huangxiaocong 2018年12月16日
     */
    public void fillTableData(XWPFTable table, List<List<Object>> tableData) {
        List<XWPFTableRow> rowList = table.getRows();
        for(int i = 0; i < tableData.size(); i++) {
            List<Object> list = tableData.get(i);
            List<XWPFTableCell> cellList = rowList.get(i).getTableCells();
            for(int j = 0; j < list.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                XWPFRun cellParagraphRun = cellParagraph.getRuns().get(0);
                cellParagraphRun.setText(String.valueOf(list.get(j)));
            }
        }
    }


}
